import axios from 'axios'
import { Notification, MessageBox, Message, Loading } from 'element-ui'
import { saveAs } from 'file-saver'
export function downloadTemplate(templateName, subURL) {
    // element-ui的loading
    const loading = Loading.service({ text: "正在下载数据，请稍候", spinner: "el-icon-loading", background: "rgba(0, 0, 0, 0.7)", })
    // axios请求
    axios({
      method: 'post',
      url: process.env.VUE_APP_BASE_API + subURL,
      data: {
        // 携带的参数
        data: ''
      },
      responseType: 'blob'
    }).then(res => {
      // 打印接口数据
      console.log(res)
      // 请求结束关闭loading
      loading.close()
      // 创建a标签
      const elink = document.createElement('a')
    //   构建Blob对象
      const blob = new Blob([res.data], { type: 'application/vnd.ms-excel; charset=utf-8' })
      // 模拟下载
      const objectUrl = URL.createObjectURL(blob)
      elink.setAttribute('href', objectUrl)
      elink.setAttribute('download', templateName + '.xlsx')
      elink.click()
      document.body.removeChild(elink)
   
})

  
}
export function downloadBlob(res, name, fixed) {
const link = document.createElement("a");
 let _fileName = null;
name ? (_fileName = name.match(/_(\S*)./)[1]) : (_fileName = fixed);
// const blob = new Blob([res], { type: 'application/vnd.ms-excel; charset=utf-8' })

   let blob = res; //如果后台返回的直接是blob对象类型，直接获取数据
    link.style.display = "none";
    // 兼容不同浏览器的URL对象
    const url = window.URL || window.webkitURL || window.moxURL;
   link.href = url.createObjectURL(blob);
    // link.href = url.createObjectURL(new Blob([blob],{type:'application/xlsx'}))
   link.download = _fileName; //下载的文件名称
     link.click();
    window.URL.revokeObjectURL(url);
    }
export function exportFile(data, type, fileName) {
        const blob = new Blob([data], {
            // type类型后端返回来的数据中会有，根据自己实际进行修改
            // 表格下载为 application/xlsx，压缩包为 application/zip等，
            type: type
        })
        const filename = fileName
        if (typeof window.navigator.msSaveBlob !== 'undefined') {
            window.navigator.msSaveBlob(blob, filename)
        } else {
            var blobURL = window.URL.createObjectURL(blob)
            // 创建隐藏<a>标签进行下载
            var tempLink = document.createElement('a')
            tempLink.style.display = 'none'
            tempLink.href = blobURL
            tempLink.setAttribute('download', filename)
            if (typeof tempLink.download === 'undefined') {
                tempLink.setAttribute('target', '_blank')
            }
            document.body.appendChild(tempLink)
            tempLink.click()
            document.body.removeChild(tempLink)// 移除dom元素
            window.URL.revokeObjectURL(blobURL)// 释放bolb内存
        }
    }